// Agent/ArchiveFolder.cpp

#include "StdAfx.h"

#include "Common/Archive/ArchiveExtractCallback.h"

#include "Agent.h"

#include "Common/Defs.h"

namespace Common
{
namespace Agent
{

STDMETHODIMP CAgentFolder::CopyTo(const UINT32 *indices, UINT32 numItems,
    const wchar_t *path, IFolderOperationsExtractCallback *callback)
{
	try
	{
		Common::Archive::CArchiveExtractCallback *extractCallbackSpec = new
			CComObject<Common::Archive::CArchiveExtractCallback>;
		extractCallbackSpec->FinalConstruct();
		CComPtr<IArchiveExtractCallback> extractCallback = extractCallbackSpec;
		CStringVector pathParts;
		auto currentProxyFolder = _proxyFolderItem;
		while (currentProxyFolder->Parent)
		{
			pathParts.push_front(currentProxyFolder->Name);
			currentProxyFolder = currentProxyFolder->Parent;
		}

		CComPtr<IFolderArchiveExtractCallback> extractCallback2;
		{
			CComPtr<IFolderOperationsExtractCallback> callbackWrap = callback;
			HRESULT hRes(callbackWrap.QueryInterface(&extractCallback2));
			if(S_OK != hRes)
				return hRes;
		}

		Common::Archive::Extract::PathMode pathMode = _flatMode ?
			Common::Archive::Extract::PathMode::kNoPathnames :
			Common::Archive::Extract::PathMode::kCurrentPathnames;

		extractCallbackSpec->InitForMulti(false, pathMode, Common::Archive::Extract::OverwriteMode::kAskBefore);
		extractCallbackSpec->Init(NULL, _agentSpec->GetArc(),
			extractCallback2,
			false, false, false,
			path, pathParts, (UINT64)(INT64)-1);
		std::vector<UINT32> realIndices;
		GetRealIndices(indices, numItems, realIndices);
		return _agentSpec->GetArchive()->Extract(&realIndices.front(),
			realIndices.size(), BoolToInt(false), extractCallback);
	} catch(...) 
	{ return E_OUTOFMEMORY; }
}

STDMETHODIMP CAgentFolder::MoveTo(const UINT32 * /* indices */, UINT32 /* numItems */,
    const wchar_t * /* path */, IFolderOperationsExtractCallback * /* callback */)
{
  return E_NOTIMPL;
}

}}